package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/1/25  Time:下午 2:59.
 * description: ** .
 */
public class CGLoadView extends View {
    private Paint mPaint;
    private Bitmap mBitmap;
    private Rect mSrcRect, mDestRect;
    private int progress=0;
    private int maxProgress=100;
    private RectF oval;
    private float strokeWidth=SizeUtils.dp2px(5);
    public CGLoadView(Context context) {
        this(context,null);
    }

    public CGLoadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CGLoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mBitmap=((BitmapDrawable)(context.getResources().getDrawable(R.drawable.ic_cg_load_img))).getBitmap();
        mSrcRect=new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = specSize;
        if (specMode == MeasureSpec.AT_MOST) {
            result = SizeUtils.dp2px(150);
        } else {
            result = specSize;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = specSize;
        if (specMode == MeasureSpec.AT_MOST) {
            result =  SizeUtils.dp2px(150);
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }
    private LinearGradient mShader;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mDestRect==null){
            mDestRect=new Rect(0,0,getWidth(),getHeight());
        }
        canvas.drawBitmap(mBitmap,mSrcRect,mDestRect,mPaint);
        float pro=getPro();
        if(pro>0){
            if(oval==null){
                oval=new RectF(getWidth()*45f/210f+strokeWidth/2,getHeight()*30f/210f+strokeWidth/2,getWidth()*165f/210f-strokeWidth/2,getHeight()*150f/210f-strokeWidth/2);
                mShader = new LinearGradient(0,oval.top, oval.left, oval.bottom, 0xff4A9FFA, 0xff47D6DE, Shader.TileMode.CLAMP);
            }
            float sweepAngle=pro*360f;
            mPaint.setShader(mShader);
            canvas.drawArc(oval, -90, sweepAngle, false, mPaint);

        }
    }
    private float getPro(){
        if(progress>maxProgress){
            progress=maxProgress;
        }
        return progress*1.0f/maxProgress;
    }
    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }
}
